(function (angular) {
    'use strict';

    var module = angular.module('sinbad.controllers.planpage', ['ngResource']);

    module.controller('PlanpageTabCtrl', ['$scope', '$uibModal', function ($scope, $modal) {
        $scope.markets = [
            {
                condition: 1,
                optimize: 1,
                content_img: "http://placehold.it/80x80",
                content: "2016网店SEO宝贝标题优化 优质关键词搜索引流 纯人工专业团队",
                bid: 2,
                move_bid: 50,
                show_amount: 543,
                click_amount: 342,
                click_rate: 0.4,
                spend: 4354,
                transaction_amount: 546,
                conversion_rate: 0.33,
                io_ratio: 4.3,
                transaction_number: 343
            },
            {
                condition: 2,
                optimize: 2,
                content_img: "http://placehold.it/80x80",
                content: "2016网店SEO宝贝标题优化 优质关键词搜索引流 纯人工专业团队",
                bid: 1,
                move_bid: 30,
                show_amount: 4332,
                click_amount: 344,
                click_rate: 0.2,
                spend: 432,
                transaction_amount: 432,
                conversion_rate: 0.54,
                io_ratio: 3.2,
                transaction_number: 233
            }
        ];
        $scope.addmoredata = function () {
            var modaInstance = $modal.open({
                templateUrl: 'views/promote/promote_moreword.html',
                controller: 'PromoredataCtrl',
                size: 500,
                backdrop: 'static',
                resolve: {
                    promoredata: function () {
                        return $scope.promoredata;

                    }
                }
            });

            modaInstance.result.then(function (promoredata) {
                    console.log('promoredata', promoredata);
                },
                function () {

                });
        };

    }]);
    module.controller('PlanpageCtrl', ['$scope', '$uibModal', function ($scope, $modal) {
        $scope.planpage = {
            autoType: 2
        };
        $scope.openPlanpageNew = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/planpage/planpage_new.html',
                controller: 'PlanpageNewCtrl',
                size: 200,
                resolve: {
                    planpage: function () {
                        return $scope.planpage;
                    }
                }
            });

            modalInstance.result.then(function (planpage) {
                console.log('planpage', planpage);
                $scope.planpage = planpage;
            }, function () {

            });
        };
        $scope.openPlanpagePlatform = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/planpage/planpage_platform.html',
                controller: 'PlanpagePlatformCtrl',
                size: 200,
                resolve: {
                    planpage: function () {
                        return $scope.planpage;
                    }
                }
            });

            modalInstance.result.then(function (planpage) {
                console.log('planpage', planpage);
                $scope.planpage = planpage;
            }, function () {

            });
        };
        $scope.openPlanpageTime = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/planpage/planpage_time.html',
                controller: 'PlanpageTimeCtrl',
                size: 200,
                resolve: {
                    planpage: function () {
                        return $scope.planpage;
                    }
                }
            });

            modalInstance.result.then(function (planpage) {
                console.log('planpage', planpage);
                $scope.planpage = planpage;
            }, function () {

            });
        };
        $scope.openPlanpageRegional = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/planpage/planpage_regional.html',
                controller: 'PlanpageRegionalCtrl',
                size: 200,
                resolve: {
                    planpage: function () {
                        return $scope.planpage;
                    }
                }
            });

            modalInstance.result.then(function (planpage) {
                console.log('planpage', planpage);
                $scope.planpage = planpage;
            }, function () {

            });
        };
    }]);

    module.controller('PlanpageNewCtrl', ['$scope', '$uibModalInstance', 'planpage',
        function ($scope, $uibModalInstance, planpage) {
            $scope.planpages = [{
                rank: 1,
                baby: '补邮费差价',
                number: 454545
            },
                {
                    rank: 2,
                    baby: 'SONY Z5',
                    number: 45425
                },
                {
                    rank: 3,
                    baby: 'SONY X',
                    number: 65645
                },
                {
                    rank: 4,
                    baby: 'SONY Z5',
                    number: 45425
                },
                {
                    rank: 5,
                    baby: 'SONY X',
                    number: 65645
                }
            ];
            $scope.planpages2 = [{
                rank: 1,
                baby: '补邮费差价',
                average_value: 45.1561
            },
                {
                    rank: 2,
                    baby: 'SONY Z5',
                    average_value: 35.1561
                },
                {
                    rank: 3,
                    baby: 'SONY X',
                    average_value: 25.1561
                },
                {
                    rank: 4,
                    baby: 'SONY Z5',
                    average_value: 15.1561
                },
                {
                    rank: 5,
                    baby: 'SONY X',
                    average_value: 4.1561
                }
            ];
            $scope.planpages3 = [{
                rank: 1,
                baby: '补邮费差价',
                numerical: 4
            },
                {
                    rank: 2,
                    baby: 'SONY Z5',
                    numerical: 3.1
                },
                {
                    rank: 3,
                    baby: 'SONY X',
                    numerical: 5
                },
                {
                    rank: 4,
                    baby: 'SONY Z5',
                    numerical: 15
                },
                {
                    rank: 5,
                    baby: 'SONY X',
                    numerical: 6
                }
            ];
            $scope.planpage = planpage;
            $scope.ok = function () {
                $uibModalInstance.close($scope.planpage);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);
    module.controller('PlanpagePlatformCtrl', ['$scope', '$uibModalInstance', 'planpage',
        function ($scope, $uibModalInstance, planpage) {
            $scope.planpage = planpage;
            $scope.ok = function () {
                $uibModalInstance.close($scope.planpage);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);
    module.controller('PlanpageTimeCtrl', ['$scope', '$uibModalInstance', 'planpage',
        function ($scope, $uibModalInstance, planpage) {
            $scope.planpage = planpage;
            $scope.ok = function () {
                $uibModalInstance.close($scope.planpage);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);
    module.controller('PlanpageRegionalCtrl', ['$scope', '$uibModalInstance', 'planpage',
        function ($scope, $uibModalInstance, planpage) {
            $scope.planpage = planpage;
            $scope.ok = function () {
                $uibModalInstance.close($scope.planpage);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
            $scope.a = function () {
                $scope.all = true
            };
            $scope.b = function () {
                $scope.all = false
            };
        }]);


    module.controller('PlanpageShiCtrl', ['$scope', '$uibModal', function ($scope, $modal) {
        $scope.planpage_shis = [
            {
                id: 12,
                name: "思路1",
                conditions: "对最近一周点击率<1倍行业均值展现量<1倍最大值的关键字执行删除操作",
                time: "2016-03-12",
                source: "导入",
                founder: "千芝",
                type: "引流包"
            },
            {
                id: 23,
                name: "思路2",
                conditions: "对最近一周点击率<1倍行业均值展现量<1倍最大值的关键字执行删除操作",
                time: "2016-02-12",
                source: "新建",
                founder: "兰芝",
                type: "增值包"
            }
        ];
        $scope.planpage_shi = {};
        $scope.operations = [
            {
                id: 1,
                keyword: "腊肉 四川 特产",
                operate: 1,
                prime: 0.8,
                optimize: 1,
                analysis: "全网表现不好，删除换词",
                score: 4,
                amount: 0,
                click: 0,
                rate: 0,
                total: 0,
                average: 0
            },
            {
                id: 2,
                keyword: "腊肉 四川 特产",
                operate: 1,
                prime: 0.8,
                optimize: 1,
                analysis: "全网表现不好，删除换词",
                score: 4,
                amount: 0,
                click: 0,
                rate: 0,
                total: 0,
                average: 0
            }
        ];
        $scope.semiauto = {};


        $scope.openBagEditor = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/planpage/planpage_semiautos.html',
                controller: 'BagEditorCtrl',
                size: 300,
                resolve: {
                    planpage_bag: function () {
                        return $scope.planpage_bag;
                    }
                }
            });

            modalInstance.result.then(function (planpage_bag) {
                console.log('planpage_bag', planpage_bag);
                var modalInstance = $modal.open({
                    templateUrl: 'views/planpage/planpage_semiautos_next.html',
                    controller: 'BagEditorCtrl',
                    size: 300,
                    resolve: {
                        planpage_bag: function () {
                            return $scope.semiauto;
                        }
                    }
                });

                modalInstance.result.then(function (semiauto) {
                    console.log('semiauto', semiauto);
                    $scope.semiautos.push(semiauto);
                    $scope.semiauto = {};
                }, function () {
                    $scope.openBagEditor();
                });
                $scope.planpage_bags.push(planpage_bag);
                $scope.planpage_bag = {};
            }, function () {

            });
        };


        $scope.openSemiautosEditor = function () {
            var step1 = $modal.open({
                templateUrl: 'views/planpage/planpage_editor.html',
                controller: 'SemiautesEditorCtrl',
                size: 300,
                resolve: {
                    semiauto: function () {
                        return $scope.semiauto;
                    },
                    operations: function () {
                        return $scope.operations;
                    }
                }
            });

            step1.result.then(function (semiauto, operations) {
                console.log('semiauto', semiauto);
                console.log('operation', operations);
                var step2 = $modal.open({
                    templateUrl: 'views/planpage/planpage_editor_next.html',
                    controller: 'SemiautesEditorCtrl',
                    size: 300,
                    resolve: {
                        semiauto: function () {
                            return $scope.semiauto;
                        },
                        operations: function () {
                            return $scope.operations;
                        }
                    }
                });
                step2.result.then(function (semiauto, operations) {
                    console.log('semiauto', semiauto);
                    $scope.semiauto = {};
                }, function () {
                    $scope.openSemiautosEditor();
                });
                $scope.semiauto = {};
            }, function () {

            });


        };

        $scope.delete = function () {
            $scope.mymessage = confirm("删除该思路马上停止优化确认删除吗?");
            if (mymessage == true) {

            }
        }

    }]);
    module.controller('BagEditorCtrl', ['$scope', '$uibModalInstance', 'planpage_bag',
        function ($scope, $uibModalInstance, planpage_bag) {
            $scope.planpage_bags = [
                {
                    bag1: 'http://placehold.it/200x200',
                    bag2: 'http://placehold.it/200x200',
                    bag3: 'http://placehold.it/200x200'
                },
                {
                    bag1: 'http://placehold.it/200x200',
                    bag2: 'http://placehold.it/200x200',
                    bag3: 'http://placehold.it/200x200'
                }
            ];

            $scope.semiautos = [
                {
                    id: 323,
                    name: "思路1",
                    condition: "推广计划",
                    time: "2016-03-26",
                    founder: "千芝",
                    type: "精确引流"
                },
                {
                    id: 324,
                    name: "思路2",
                    condition: "推广计划",
                    time: "2016-03-26",
                    founder: "兰芝",
                    type: "精确引流"
                }
            ];


            $scope.addCondition = function (method) {
                var condition = {
                    name: '1',
                    condition: '=',
                    value: 100,
                    source_type: 1,
                    source_value: 1,
                    method: method
                };
                $scope.semiauto.conditions.push(condition);
            };

            $scope.ok = function () {
                $uibModalInstance.close($scope.planpage_bag);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }

    ]);
    module.controller('SemiautesEditorCtrl', ['$scope', '$uibModalInstance', 'semiauto', 'operations',
        function ($scope, $uibModalInstance, semiauto, operations) {
            $scope.semiauto = semiauto;
            $scope.semiauto.id = '000';
            $scope.semiauto.name = "思路";
            $scope.semiauto.platform = $scope.semiauto.platform || '1';
            $scope.semiauto.days = $scope.semiauto.days || '1';
            $scope.semiauto.time = new Date();
            $scope.semiauto.type = $scope.semiauto.type || '1';
            $scope.semiauto.symbol = $scope.semiauto.symbol || '1';

            $scope.semiauto.conditions = [
                {
                    name: '1',
                    condition: '=',
                    value: 1,
                    source_type: 1,
                    source_value: 1,
                    method: 'and'
                }
            ];

            $scope.semiauto.plans = [
                {
                    name: "淘宝天猫店铺直通车托管 流量优化整店代运营 专业团队纯人工服务",
                    price: 2000,
                    type: "",
                    time: "2016-02-29 11:00:40",
                    sales: 34
                },
                {
                    name: "网店pc端无线端装修 首页设计详情页设计爆款页设计 可包上传",
                    price: 1000,
                    type: "",
                    time: "2016-02-29 11:07:56",
                    sales: 0
                },
                {
                    name: "2016网店钻展托管服务 dmp精准定向投放爆款运营纯人工可包月",
                    price: 3000,
                    type: "",
                    time: "2016-02-29 11:05:15",
                    sales: 0
                },
                {
                    name: "2016网店SEO宝贝标题优化 优质关键词搜索引流 纯人工专业团队",
                    price: 600,
                    type: "",
                    time: "2016-02-29 11:02:45",
                    sales: 0
                }
            ];

            $scope.addCondition = function (method) {
                var condition = {
                    name: '1',
                    condition: '=',
                    value: 100,
                    source_type: 1,
                    source_value: 1,
                    method: method
                };
                $scope.semiauto.conditions.push(condition);
            };

            $scope.semiauto.match_model =
            {
                status: 0,
                value: 0
            };

            $scope.semiauto.price_model =
            {
                status: 0,
                price_type: 1,
                option_type: 1,
                price_value: 0
            };

            $scope.pricing = ( $scope.semiauto.price_model.status || $scope.semiauto.match_model.status) ? true : false;

            $scope.aim1 = function () {
                $scope.aim = false;
            };

            $scope.aim2 = function () {
                $scope.aim = true;
            };

            $scope.handle_plan = function (plan) {
                var planaim = plan.num[$index];
                for (var i = 0; i < planaim.length; i++) {
                    if ($scope.plan.num[$index] == checked) {
                        console.log('semiauto.plan[$index]', plan);
                    }
                }
                $scope.aim = false;


                $scope.semiauto.plans.push(plan);
            };

            $scope.operations = operations;

            $scope.ok = function () {
                $uibModalInstance.close($scope.semiauto, $scope.operations);
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);
    module.controller('Promoredata1Ctrl', ['$scope', '$uibModalInstance', ' promoredata',
        function ($scope, $uibModalInstance, promoredata) {
            $scope.promoredata = promoredata;
            $scope.ok = function () {
                $uibModalInstance.close($scope.promoredata);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel')

            };


        }]);
})(angular);